// ## 2023/11/25 #铃# 感谢墨玉丹青的UI设计,重新制作了UI界面 ##
// ## 2023/5/09 #铃# 上个版本修复未生效,再次修复 ##
// ## 2023/5/09 #铃# 降低了资源调度的资源下限(3000→1000) ##
// ## 2023/3/19 #铃# 修复了运输时计算附近兵力错误的bug ##
// ## 2023/3/19 #铃# 結合新的內政系統,新建了玩家的府资源调度功能 ##

namespace 县城运输
{
	const int ACTION_COST = 20; // 行动力消耗
	const array<string> 按钮文本 = {"士兵", "金币", "粮食"};
	const int troops_min = 1000;
	const int gold_min = 1000;
	const int food_min = 3000;
	class Main
	{
		pk::building @building_spec;
		pk::force @force;
		pk::building @kunshu_building;
		pk::building @city_;
		pk::point src_pos;
		int spec_id;
		int spec_troops;
		int spec_food;
		int spec_gold;
		int spec_troops_transpot;
		int spec_food_transpot;
		int spec_gold_transpot;
		int city_id;
		int city_troops;
		int city_food;
		int city_gold;
		int temp_troops;
		int temp_food;
		int temp_gold;
		int select_type;
		int lost_ratio;
		pk::dialog @main_dialog;
		array<pk::button @> button_wuzi(3, null); // 士兵,资金,兵粮
		array<pk::text @> text_wuzi(3, null);	  // 士兵,资金,兵粮
		array<pk::slider @> troop_slider_all(3, null);
		array<pk::button @> button_wuzi_min(3, null); // 士兵,资金,兵粮
		array<pk::button @> button_wuzi_mid(3, null); // 士兵,资金,兵粮
		array<pk::button @> button_wuzi_max(3, null); // 士兵,资金,兵粮
		Main()
		{
			add_menu();
		}

		void add_menu()
		{
			pk::menu_item item;
			item.menu = 0;
			item.pos = 10;
			item.init = pk::building_menu_item_init_t(init_县城运输);
			item.is_visible = pk::menu_item_is_visible_t(isVisible_县城运输);
			item.is_enabled = pk::menu_item_is_enabled_t(isEnabled_县城运输);
			item.get_text = pk::menu_item_get_text_t(getText_县城运输);
			item.get_desc = pk::menu_item_get_desc_t(getDesc_县城运输);
			item.handler = pk::menu_item_handler_t(handler_县城运输);
			pk::add_menu_item(item);
		}

		void init_县城运输(pk::building @building)
		{
			@building_spec = @building;
			@force = pk::get_force(building.get_force_id());
			if (pk::is_valid_person_id(force.kunshu))
			{
				@kunshu_building = pk::get_building(pk::get_person(force.kunshu).service);
			}

			src_pos = building.get_pos();
			int spec_id = ch::to_spec_id(building_spec.get_id());
			if (!ch::is_valid_spec_id(spec_id)) return ;
			int city_id = pk::get_building_id(building.pos);
			@city_ = pk::get_building(city_id);
			city_troops = city_.troops;
			city_food = pk::get_food(city_);
			city_gold = pk::get_gold(city_);

			spec_troops = ch::get_spec_p(spec_id).troops;
			spec_food = ch::get_spec_p(spec_id).food;
			spec_gold = ch::get_spec_p(spec_id).gold;
		}

		bool isVisible_县城运输()
		{
			if (!building_spec.is_player())
				return false;

			int spec_id = ch::to_spec_id(building_spec.get_id());
			if (!ch::is_valid_spec_id(spec_id))
				return false;

			int force_id = building_spec.get_force_id();
			if (!pk::is_valid_force_id(force_id) or pk::get_current_turn_force_id() != force_id)
				return false;

			if (!pk::is_valid_district_id(kunshu_building.get_district_id()))
				return false;

			int spec_person = ch::get_spec_p(spec_id).person;
			if (!pk::is_valid_person_id(spec_person))
				return false;

			return true;
		}

		bool isEnabled_县城运输()
		{

			if (check_县城运输(building_spec) == 0)
				return true;
			else
				return false;
		}

		int check_县城运输(pk::building @building)
		{
			if (building is null or !pk::is_alive(building))
				return 3;
			int spec_id = ch::to_spec_id(building.get_id());
			if (!ch::is_valid_spec_id(spec_id))
				return 4;
			if (pk::get_district(kunshu_building.get_district_id()).ap < ACTION_COST)
				return 1;
			int city_id = pk::get_building_id(building.pos);
			if (pk::get_building(city_id).get_force_id() != force.get_id())
				return 2;
			return 0;
		}

		string getText_县城运输()
		{
			return pk::encode("县城运输");
		}

		string getDesc_县城运输()
		{
			int result = check_县城运输(building_spec);
			switch (result)
			{
			case 1:
				return pk::encode(pk::format("行动力不足 (必须 {} 行动力)", ACTION_COST));
			case 2:
				return pk::encode("主城不是同一势力");
			case 3:
				return pk::encode("无效建筑");
			case 4:
				return pk::encode("无效ID");
			default:
				return pk::encode("");
			}
			return pk::encode("");
		}

		bool handler_县城运输()
		{

			main.select_type = -1;
			int city_id = pk::get_building_id(building_spec.pos);
			pk::building @city = pk::get_building(city_id);
			pk::building @building0 = pk::get_building(city_id);
			pk::person @kunshu = pk::get_person(pk::get_kunshu_id(city));
			int spec_id = ch::to_spec_id(building_spec.get_id());
			main.spec_id = spec_id;
			string src_name = ch::get_spec_name(spec_id);
			string city_name = pk::decode(pk::get_name(city));
			int person_id = ch::get_spec_p(spec_id).person;
			int ap = pk::get_district(kunshu_building.get_district_id()).ap;

			int spec_gold = ch::get_spec_p(spec_id).gold;
			int spec_food = ch::get_spec_p(spec_id).food;
			int spec_troops = ch::get_spec_p(spec_id).troops;

			main.lost_ratio = 计算敌方部队(building_spec) * 10 + int(计算敌方兵力(building_spec) / 2000) + 计算敌方部队(building0) * 10 + int(计算敌方兵力(building0) / 2000);
			main.lost_ratio = pk::clamp(lost_ratio, 0, 99);

			if (计算敌方部队(building_spec) == 计算有效地形(building_spec))
			{
				lost_ratio = 99;
				pk::message_box(pk::encode(pk::format("\x1b[2x{}\x1b[0x已无路可退,强行运输会产生大约\x1b[2x{}%\x1b[0x的lost_ratio", src_name, main.lost_ratio)));
				bool confirm = pk::yes_no(pk::encode("是否还要运输??"));
				if (!confirm)
					return true;
			}

			if (计算敌方部队(building_spec) + 计算敌方部队(building0) > 0)
				pk::message_box(pk::encode(pk::format("向\x1b[2x{}\x1b[0x的运输路线附近有敌部队,强行运输会产生大约\x1b[2x{}%\x1b[0x的损失", city_name, main.lost_ratio)));
			else
				pk::message_box(pk::encode(pk::format("\x1b[2x{}\x1b[0x的附近没有敌方部队,可以安全运输", city_name)));

			main.select_type = 1;

			main.temp_troops = 0;
			main.temp_food = 0;
			main.temp_gold = 0;
			main.spec_troops_transpot = 0;
			main.spec_food_transpot = 0;
			main.spec_gold_transpot = 0;
			int temp_troops = 0;
			int temp_food = 0;
			int temp_gold = 0;
			if (select_type == 0)
			{

				temp_troops = 30000 - main.spec_troops;
				temp_troops = pk::min(temp_troops, main.city_troops);

				temp_gold = 30000 - main.spec_gold;
				temp_gold = pk::min(temp_gold, main.city_gold);

				temp_food = 30000 - main.spec_food;
				temp_food = pk::min(temp_food, main.city_food);
			}
			else
			{

				temp_troops = main.spec_troops - troops_min;
				if (temp_troops < 0)
					temp_troops = 0;

				temp_gold = main.spec_gold - gold_min;
				if (temp_gold < 0)
					temp_gold = 0;

				temp_food = main.spec_food - food_min;
				if (temp_food < 0)
					temp_food = 0;
			}

			main.temp_troops = temp_troops;
			main.temp_gold = temp_gold;
			main.temp_food = temp_food;
			if (main.temp_troops == 0)
			{
				pk::message_box(pk::encode(pk::format("主公!\x1b[1x{}\x1b[0x暂无空闲兵力运输!", src_name)), pk::get_person(武将_文官));
				return true;
			}

			pk::dialog @dialog = pk::new_dialog();
			pk::size dialog_size = pk::size(560, 100);
			auto buttons = UI::create_dlg_320(dialog, dialog_size, pk::encode("县城向城市运输"));
			pk::button @ok_button = buttons[0];
			pk::button @esc_button = buttons[1];

			@main_dialog = @dialog;
			dialog.set_pos(pk::get_resolution().width / 2 - dialog_size.width / 2, pk::get_resolution().height / 2 - dialog_size.height / 2 - 60);
			pk::detail::funcref func0 = cast<pk::button_on_released_t @>(function(button) {
				main.spec_troops_transpot = 0;
				main.main_dialog.close(1);
			});
			pk::detail::funcref func1 = cast<pk::button_on_released_t @>(function(button) {
				if (main.spec_troops_transpot == 0)
				{
					pk::message_box(pk::encode("主公!\x1b[16x没有士兵\x1b[0x如何运输?\n\x1b[17x(运兵数不能为0)"), pk::get_person(武将_文官));
					return;
				}	
				if (main.select_type == 0)
				{
					pk::add_troops(main.city_, -main.spec_troops_transpot, true);
					pk::add_gold(main.city_, -main.spec_gold_transpot, true);
					pk::add_food(main.city_, -main.spec_food_transpot, true);

					ch::get_spec_p(main.spec_id).troops += int(main.spec_troops_transpot * (100 - main.lost_ratio) / 100);
					ch::get_spec_p(main.spec_id).gold += int(main.spec_gold_transpot * (100 - main.lost_ratio) / 100);
					ch::get_spec_p(main.spec_id).food += int(main.spec_food_transpot * (100 - main.lost_ratio) / 100);
				}
				else
				{
					pk::add_troops(main.city_, int(main.spec_troops_transpot * (100 - main.lost_ratio) / 100), true);
					pk::add_gold(main.city_, int(main.spec_gold_transpot * (100 - main.lost_ratio) / 100), true);
					pk::add_food(main.city_, int(main.spec_food_transpot * (100 - main.lost_ratio) / 100), true);

					ch::get_spec_p(main.spec_id).troops -= main.spec_troops_transpot;
					ch::get_spec_p(main.spec_id).gold -= main.spec_gold_transpot;
					ch::get_spec_p(main.spec_id).food -= main.spec_food_transpot;
				}
				main.main_dialog.close(1);
			});
			ok_button.on_button_released(func1);
			esc_button.on_button_released(func0);
			ok_button.set_text(pk::encode("调度"));
			esc_button.set_text(pk::encode("退出"));

			for (int number = 0; number < 3; number++)
			{

				int x_slider = 80;
				int y_slider = 40 + number * 28 + 30;

				pk::sprite9 @imgb = main_dialog.create_sprite9(393);
				imgb.set_size(pk::size(100, 24));
				imgb.set_pos(x_slider + 256, y_slider - 4);
				pk::button @button_slider = main_dialog.create_button(1075);
				button_slider.set_pos(x_slider - 50, y_slider - 3);
				button_slider.set_size(pk::size(40, 26));
				button_slider.set_text(pk::encode(按钮文本[number]));
				@button_wuzi[number] = button_slider;
				pk::text @text_slider2_stemp = main_dialog.create_text();
				text_slider2_stemp.set_pos(x_slider + 259, y_slider);
				text_slider2_stemp.set_size(pk::size(90, 16));

				switch (number)
				{
				case 0:
					text_slider2_stemp.set_text(pk::encode(main.spec_troops_transpot + "/" + temp_troops));
					break;
				case 1:
					text_slider2_stemp.set_text(pk::encode(main.spec_gold_transpot + "/" + temp_gold));
					break;
				case 2:
					text_slider2_stemp.set_text(pk::encode(main.spec_food_transpot + "/" + temp_food));
					break;
				}

				text_slider2_stemp.set_text_align(4);
				@text_wuzi[number] = text_slider2_stemp;

				pk::sprite9 @slider_imgb = main_dialog.create_sprite9(608);
				slider_imgb.set_size(pk::size(254, 20));
				slider_imgb.set_pos(pk::point(x_slider - 2, y_slider - 2));

				pk::slider @troop_slider = main_dialog.create_slider();
				troop_slider.set_pos(pk::point(x_slider, y_slider));
				troop_slider.set_size(pk::size(250, 16));
				troop_slider.set_value(0);
				switch (number)
				{
				case 0:
					troop_slider.set_min_max(0, temp_troops);
					break;
				case 1:
					troop_slider.set_min_max(0, temp_gold);
					break;
				case 2:
					troop_slider.set_min_max(0, temp_food);
					break;
				}
				@troop_slider_all[number] = troop_slider;

				pk::button@ button_wuzi_min_btn = main_dialog.create_button(1481);
				button_wuzi_min_btn.set_pos(pk::point(x_slider - 2 + 360, y_slider - 4));
				button_wuzi_min_btn.set_size(pk::size(40, 26));
				button_wuzi_min_btn.set_text(pk::encode("最小"));
				@button_wuzi_min[number] = button_wuzi_min_btn;

				pk::button@ button_wuzi_mid_btn = main_dialog.create_button(1480);
				button_wuzi_mid_btn.set_pos(pk::point(x_slider - 2 + 360 + 43, y_slider - 4));
				button_wuzi_mid_btn.set_size(pk::size(40, 26));
				button_wuzi_mid_btn.set_text(pk::encode("一半"));
				@button_wuzi_mid[number] = button_wuzi_mid_btn;

				pk::button@ button_wuzi_max_btn = main_dialog.create_button(1478);
				button_wuzi_max_btn.set_pos(pk::point(x_slider - 2 + 360 + 86, y_slider - 4));
				button_wuzi_max_btn.set_size(pk::size(40, 26));
				button_wuzi_max_btn.set_text(pk::encode("最大"));
				@button_wuzi_max[number] = button_wuzi_max_btn;
				
			}
			pk::detail::funcref funcslider0 = cast<pk::slider_on_value_changed_t @>(function(self, value) {
				main.text_wuzi[0].set_text(pk::format("{}/{}", value, main.temp_troops));
				main.spec_troops_transpot = value;
			});
			troop_slider_all[0].on_slider_value_changed(funcslider0);

			pk::detail::funcref funcslider1 = cast<pk::slider_on_value_changed_t @>(function(self, value) {
				main.text_wuzi[1].set_text(pk::format("{}/{}", value, main.temp_gold));
				main.spec_gold_transpot = value;
			});
			troop_slider_all[1].on_slider_value_changed(funcslider1);

			pk::detail::funcref funcslider2 = cast<pk::slider_on_value_changed_t @>(function(self, value) {
				main.text_wuzi[2].set_text(pk::format("{}/{}", value, main.temp_food));
				main.spec_food_transpot = value;
			});
			troop_slider_all[2].on_slider_value_changed(funcslider2);

			pk::detail::funcref funcslider_max_0 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[0].set_text(pk::format("{}/{}", main.temp_troops, main.temp_troops));
				main.spec_troops_transpot = main.temp_troops;
				main.troop_slider_all[0].set_value(main.spec_troops_transpot);
			});
			button_wuzi_max[0].on_button_released(funcslider_max_0);

			pk::detail::funcref funcslider_max_1 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[1].set_text(pk::format("{}/{}", main.temp_gold, main.temp_gold));
				main.spec_gold_transpot = main.temp_gold;
				main.troop_slider_all[1].set_value(main.spec_gold_transpot);
			});
			button_wuzi_max[1].on_button_released(funcslider_max_1);

			pk::detail::funcref funcslider_max_2 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[2].set_text(pk::format("{}/{}", main.temp_food, main.temp_food));
				main.spec_food_transpot = main.temp_food;
				main.troop_slider_all[2].set_value(main.spec_food_transpot);
			});
			button_wuzi_max[2].on_button_released(funcslider_max_2);

			pk::detail::funcref funcslider_mid_0 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[0].set_text(pk::format("{}/{}", main.temp_troops, main.temp_troops));
				if (main.temp_troops > 0)
					main.spec_troops_transpot = int(main.temp_troops / 2);
				main.troop_slider_all[0].set_value(main.spec_troops_transpot);
			});
			button_wuzi_mid[0].on_button_released(funcslider_mid_0);

			pk::detail::funcref funcslider_mid_1 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[1].set_text(pk::format("{}/{}", main.temp_gold, main.temp_gold));
				if (main.temp_gold > 0)
					main.spec_gold_transpot = int(main.temp_gold / 2);
				main.troop_slider_all[1].set_value(main.spec_gold_transpot);
			});
			button_wuzi_mid[1].on_button_released(funcslider_mid_1);

			pk::detail::funcref funcslider_mid_2 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[2].set_text(pk::format("{}/{}", main.temp_food, main.temp_food));
				if (main.temp_gold > 0)
					main.spec_food_transpot = int(main.temp_gold / 2);
				main.troop_slider_all[2].set_value(main.spec_food_transpot);
			});
			button_wuzi_mid[2].on_button_released(funcslider_mid_2);

			pk::detail::funcref funcslider_min_0 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[0].set_text(pk::format("0/{}", main.temp_troops));
				main.spec_troops_transpot = 0;
				main.troop_slider_all[0].set_value(main.spec_troops_transpot);
			});
			button_wuzi_min[0].on_button_released(funcslider_min_0);

			pk::detail::funcref funcslider_min_1 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[1].set_text(pk::format("0/{}", main.temp_gold));
				main.spec_gold_transpot = 0;
				main.troop_slider_all[1].set_value(main.spec_gold_transpot);
			});
			button_wuzi_min[1].on_button_released(funcslider_min_1);

			pk::detail::funcref funcslider_min_2 = cast<pk::slider_on_value_changed_t@>(function(self, value) {
				main.text_wuzi[2].set_text(pk::format("0/{}", main.temp_food));
				main.spec_food_transpot = 0;
				main.troop_slider_all[2].set_value(main.spec_food_transpot);
			});
			button_wuzi_min[2].on_button_released(funcslider_min_2);

			open_dlg(dialog);
			if (main.spec_troops_transpot > 0)
				府兵运输队(building_spec, building0, person_id, main.spec_troops_transpot, main.spec_gold_transpot, main.spec_food_transpot);

			return true;
		}

		void open_dlg(pk::dialog @dialog)
		{
			dialog.open();
			dialog.delete();
		}

		int 计算敌方部队(pk::building @building)
		{
			int enemy_units = 0;
			auto range = pk::range(building.get_pos(), 1, 1);
			for (int i = 0; i < int(range.length); i++)
			{
				auto unit = pk::get_unit(range[i]);
				if (pk::is_alive(unit) and pk::is_enemy(building, unit))
				{

					enemy_units++;
					// pk::trace(pk::format("range.length：{},unit.troops:{},enemy_troops:{},enemy_unit:{}", range.length, unit.troops, enemy_troops,enemy_units));
				}
			}
			return enemy_units;
		}

		int 计算有效地形(pk::building @building)
		{
			int valid_pos_count = 0;

			auto range = pk::range(building.get_pos(), 1, 3);
			for (int i = 0; i < int(range.length); i++)
			{
				pk::hex @hex = pk::get_hex(range[i]);
				int terrain_id = hex.terrain;
				if (pk::is_enabled_terrain(terrain_id))
					valid_pos_count++;
				// pk::trace(pk::format("range.length：{},unit.troops:{},enemy_troops:{},enemy_unit:{}", range.length, unit.troops, enemy_troops,enemy_units));
			}

			return valid_pos_count;
		}

		int
		计算敌方兵力(pk::building @building)
		{

			int enemy_troops = 0;
			int enemy_units = 0;

			auto range = pk::range(building.get_pos(), 1, 3);
			for (int i = 0; i < int(range.length); i++)
			{
				auto unit = pk::get_unit(range[i]);
				if (pk::is_alive(unit) and pk::is_enemy(building, unit))
				{

					enemy_troops += unit.troops;
					// enemy_units++;
					// pk::trace(pk::format("range.length：{},unit.troops:{},enemy_troops:{},enemy_unit:{}", range.length, unit.troops, enemy_troops,enemy_units));
				}
			}
			return enemy_troops;
		}

		void 府兵运输队(pk::building @building, pk::building @city, int person_id, int troops, int gold, int food)
		{

			if (!func_person_slot_available())
				return; // 沒有空余武将位则不生成

			pk::point src_pos;
			pk::point dst_pos;
			auto arr1 = pk::range(building.get_pos(), 1, 1);
			for (int i = 0; i < int(arr1.length); i++)
			{
				pk::hex @hex = pk::get_hex(arr1[i]);
				int terrain_id = hex.terrain;

				if (!hex.has_building and !hex.has_unit and pk::is_valid_terrain_id(terrain_id) and pk::is_enabled_terrain(terrain_id) and (terrain_id <= 地形_湿地 or terrain_id == 地形_荒地 or terrain_id == 地形_官道))
				{
					src_pos = arr1[i];
					break;
				}

				if (!hex.has_building and !hex.has_unit and pk::is_valid_terrain_id(terrain_id) and pk::is_enabled_terrain(terrain_id) and terrain_id < 地形_栈道 and terrain_id != 地形_川)
					src_pos = arr1[i];
			}

			auto arr2 = pk::range(building.get_pos(), 1, 1);
			for (int i = 0; i < int(arr2.length); i++)
			{
				pk::hex @hex = pk::get_hex(arr2[i]);
				int terrain_id = hex.terrain;

				if (!hex.has_building and !hex.has_unit and pk::is_valid_terrain_id(terrain_id) and pk::is_enabled_terrain(terrain_id) and terrain_id < 地形_栈道 and terrain_id != 地形_川)
					dst_pos = arr2[i];
			}

			//	break;
			pk::person @virtual_person = func_create_person(pk::get_person(person_id));
			if (!pk::is_alive(virtual_person))
			{
				pk::reset(virtual_person);
				return;
			}
			// pk::trace(pk::format("x1 :{},y1:{},x2 :{},y2 :{}", src_pos.x, src_pos.y, dst_pos.x, dst_pos.y));

			// unit_pos = get_empty_pos(building.pos, 1, 2); // 生成部队前再次核实坐标有效

			pk::com_march_cmd_info cmd_draft;
			@cmd_draft.base = building;
			cmd_draft.type = 部队类型_运输;
			cmd_draft.member[0] = virtual_person.get_id();
			cmd_draft.gold = gold;
			cmd_draft.troops = troops;
			cmd_draft.food = food;
			cmd_draft.order = 部队任务_移动;
			cmd_draft.target_pos = dst_pos;
			cmd_draft.weapon_id[0] = 1;
			cmd_draft.weapon_amount[0] = 0;
			pk::unit @unit = pk::create_unit2(cmd_draft, src_pos, false);
			unit.energy = pk::get_energy(building);

			pk::point next_pos = get_empty_pos(city.pos, 1, 2);
			// next_pos = pk::point(182, 24);

			int distance = pk::get_distance(unit.pos, next_pos);

			if (next_pos != unit.pos)
			{
				if (pk::is_valid_pos(unit.pos) and pk::is_valid_pos(next_pos))
				{
					array<pk::point> unit_path = {unit.pos, next_pos}; // 临时措施
					if (unit_path.length < 40 and unit_path.length > 0)
						pk::move(unit, unit_path);
				}
			}
			pk::kill(unit, true);
		}

		pk::point get_empty_pos(pk::point pos, int distance_min, int distance_max)
		{
			array<pk::point> range_pos_arr = pk::range(pos, distance_min, distance_max);
			for (int arr_index = 0; arr_index < int(range_pos_arr.length); arr_index++)
			{
				pk::point range_pos = range_pos_arr[arr_index];
				if (!pk::is_valid_pos(range_pos))
					continue;

				pk::hex @hex = pk::get_hex(range_pos);
				if (hex.has_building)
					continue;
				if (hex.has_unit)
					continue;

				int terrain_id = hex.terrain;
				if (!pk::is_valid_terrain_id(terrain_id))
					continue;
				if (!pk::is_enabled_terrain(terrain_id))
					continue;

				return range_pos;
			}

			return pk::point(-1, -1);
		}

		void kill(pk::unit @unit, int spec_id)
		{
			auto spec_t = ch::get_spec_p(spec_id);
			spec_t.add_troops(unit.troops);
			spec_t.add_gold(unit.gold);
			spec_t.add_food(unit.food);
			pk::kill(unit, true);
		}

		// 遍历所有武将，身??死亡或无的都可用来生成
		bool func_person_slot_available()
		{
			for (int person_id = 敌将_开始; person_id < 敌将_末; person_id++)
			{
				pk::person @person = pk::get_person(person_id);
				if (person.mibun == 身份_无)
					return true;
				if (person.mibun == 身份_死亡)
					return true;
			}
			return false;
		}

		// 统领武将生成
		pk::person @func_create_person(pk::person @person)
		{
			pk::person @f_person = ch::create_temp_person(武将_卫士, 17);//模板武将其实无所谓，因为后续还会设置

			ch::set_spec_temp_person(f_person, person, person.get_district_id());

			return f_person;
		}

	} Main main;
}
